<template>
  <div>
    <a-card class="card-content">
      <a-row :gutter="15">
        <a-col :span="24">
          <div class="contetnBox">
            <a-row :gutter="24" class="headerBox">
              <a-col :span="10">
                <a-button type="primary" @click="handleAdd">新建</a-button>
                <!-- <a-button type="primary" @click="handleAdd">导出</a-button> -->
              </a-col>
              <a-col :span="11">
                <div class="flex flex-a-c">
                  <div style="width:100px">角色名称：</div>
                  <a-input
                    v-model.trim="queryParam.name"
                    placeholder="请输入角色名称："
                  ></a-input>
                </div>
              </a-col>
              <a-col :span="3">
                <a-button type="primary" @click="handleSearch">查询</a-button>
              </a-col>
            </a-row>
            <br />

            <a-table
              :columns="columns"
              :data-source="tableData"
              :loading="loading"
              bordered
              :pagination="pagination"
              :rowClassName="rowClassName"
              @change="handleTableChange"
              :rowKey="(record, index) => record.id"
            >
              <template slot="action" slot-scope="text, record">
                <a @click.stop="handleGrant(record)">授权</a>
                <a-divider type="vertical" v-if="record.id !== 1" />
                <a @click.stop="handleEdit(record)" v-if="record.id !== 1"
                  >编辑</a
                >
                <a-divider type="vertical" v-if="record.id !== 1" />
                <span @click.stop>
                  <a-popconfirm
                    okText="确定"
                    cancelText="取消"
                    v-if="record.id !== 1"
                    title="确定删除吗?"
                    @confirm="() => handleDelete(record)"
                  >
                    <a>删除</a>
                  </a-popconfirm>
                </span>
              </template>
            </a-table>
          </div>
        </a-col>
        <!-- <a-col :span="12">
          <Permission
            :userId="userId"
            :resources="resources"
            ref="permission"
            @ok="modalFormOk"
          />
        </a-col> -->
      </a-row>
    </a-card>
    <PermissionDrawer
      :resources="resources"
      @ok="modalFormOk"
      :userId="userId"
      ref="permission"
    ></PermissionDrawer>
    <RoleModal ref="modalForm" @ok="modalFormOk"></RoleModal>
  </div>
</template>

<script>
import { roleColumns } from "./components/tableConfig";
import { tableMinxins } from "@/unit/mixins/tableMinxins.js";
import RoleModal from "./components/RoleModal.vue";
import PermissionDrawer from "./components/PermissionDrawer.vue";
import api from "@/api/apiServer";

export default {
  components: { RoleModal, PermissionDrawer },
  mixins: [tableMinxins],
  data() {
    return {
      columns: roleColumns,
      userId: 0, // 当前选中角色ID
      resources: "", // 当前选中角色的权限
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 获取列表数据
    async getList(arg) {
      this.loading = true;

      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.pagination.current = 1;
      }
      var params = this.getQueryParams();
      const res = await api.RoleList(params);
      this.tableData = res.data.data;
      this.pagination.total = res.data.total;
      this.loading = false;
    },
    // 授权
    handleGrant(record) {
      this.userId = record.id;
      this.resources = record.resources;
      this.$refs.permission.showDrawer(this.resources);
    },
    rowClassName(record, index) {
      //选择行后设置颜色
      return record.id === this.userId ? "blue" : "white";
    },
    onSelectChange() {},
    // 删除
    async handleDelete(record) {
      let params = {
        id: record.id,
      };
      const res = await api.RoleDelete(params);
      this.$message.success(res.message);
      this.getList();
    },
  },
};
</script>

<style lang="less" scoped>
.card-content {
  margin: 20px 0;
  button {
    margin-right: 13px;
  }
  .contetnBox {
    min-height: 809px;
    border: 1px solid #ebedf0;
    border-radius: 2px;
    padding: 20px 0;
    .headerBox {
      padding: 0 20px 0 35px;
    }
  }
}

/deep/.ant-table-tbody .white {
  height: 40px !important;
  border: none !important;
  padding: 0 !important;
}
/deep/.ant-table-tbody .blue {
  background-color: rgba(39, 111, 255, 0.1) !important;
  height: 40px !important;
  border: none !important;
  padding: 0 !important;
}
</style>
